/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.iecg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iecg.api.entity.IecgApp;
import com.iecg.api.entity.IecgVersion;
import com.iecg.mapper.IecgAppMapper;
import com.iecg.service.IecgAppService;
import com.iecg.service.IecgVersionService;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * app管理
 *
 * @author pigx code generator
 * @date 2020-01-07 15:50:55
 */
@Service
@AllArgsConstructor
public class IecgAppServiceImpl extends ServiceImpl<IecgAppMapper, IecgApp> implements IecgAppService {

	private RedisTemplate redisTemplate;

    private IecgVersionService iecgVersionService;

	@Override
	public IPage<IecgApp> getPageTestList(Page<IecgApp> page, QueryWrapper query) {
		return baseMapper.getPageTestList(page, query);
	}

	@Override
	public void hitVersion(IecgApp iecgApp) {
		boolean absent = redisTemplate.opsForValue().setIfAbsent(CacheConstants.VERSION_DEVICE + ":" + iecgApp.getDeviceId() +"_" + iecgApp.getAppVersion(), 1);
		if(!absent){
			return;
		}
		long deviceNum = redisTemplate.opsForValue().increment(CacheConstants.DEVICE_NUM + ":" +iecgApp.getAppVersion());
		IecgVersion iecgVersion = new IecgVersion();
		iecgVersion.setDeviceNum(deviceNum);
		iecgVersion.setVersion(iecgApp.getAppVersion());
		iecgVersionService.update(iecgVersion, Wrappers.<IecgVersion>update().lambda().eq(IecgVersion::getVersion, iecgVersion.getVersion()));
	}
}
